package enumdemo;
import java.lang.reflect.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-03-30
 * Time: 22:40
 */
public class TestEnumReflect {
    public static void reflectPrivateConstructor(){
        try {
            Class<?> classEnum = Class.forName("enumdemo.TestEnum");

            //获取 TestEnum 类中的构造方法
            Constructor<?> comparable = classEnum.getDeclaredConstructor(String.class,int.class,String.class,int.class);

            comparable.setAccessible(true);//因为构造方法是私有的，所以要使用 setAccessible 方法修改权限

            Object objectEnum = comparable.newInstance("绿色",666,"父类参数",888); //实例化对象
            TestEnum testEnum = (TestEnum)objectEnum;
            System.out.println("获得枚举的私有构造方法："+testEnum);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        reflectPrivateConstructor();
    }
}
